<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="copyright" content="All Rights Reserved, Copyright (C) 2013, Wuyeguo, Ltd." />
<title>EasyUI Web Admin Power by Wuyeguo</title>
<link rel="stylesheet" type="text/css" href="../easyui/1.5.3/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="../css/wu.css" />
<link rel="stylesheet" type="text/css" href="../css/icon.css" />
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../js/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../js/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="../js/jquery.serializejson.min.js"></script>
<script type="text/javascript" src="../public/xheditor/xheditor-1.2.2.min.js"></script>
</head>
<body>
	 <div class="easyui-layout" data-options="fit:true">
        <div data-options="region:'west',split:true" title="West" style="width:160px;">
				<ul id="tt"></ul>
        </div>
        <div data-options="region:'center',title:'Main Title',iconCls:'icon-ok'">
        	<div class="wu-toolbar-button">
	            <a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="openUserAdd()" plain="true">添加新闻</a>
	            <a href="#" class="easyui-linkbutton" iconCls="icon-edit" onclick="openUserEdit()" plain="true">修改新闻</a>
	            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="removeUser()" plain="true">删除新闻</a>
	            <a href="#" class="easyui-linkbutton" iconCls="icon-reload" onclick="reloadUser()" plain="true">刷新</a>
	        </div>
           		
            	<table id="dga"></table>
        </div>
   </div>
<div id="user-dialog" class="easyui-dialog" data-options="closed:true,iconCls:'icon-save'" style="width:520px; padding:10px;top:0;">
	<form id="user-form" method="post">
        <table>
            <tr>
                <td width="60" align="right">标题:</td>
                <td><input type="text" name="title" class="wu-text wu-text0" /></td>
            </tr>
            <tr>
                <td align="right">作者:</td>
                <td><input type="text" name="author" class="wu-text wu-text1" /></td>
            </tr>
            <tr>
                <td align="right">副标题:</td>
                <td><input type="text" name="subhead" class="wu-text wu-text2" /></td>
            </tr>
            <tr>
                <td valign="top" align="right">内容:</td>
                <!--<textarea cols="130" rows="20" name="content" class="xheditor"></textarea><br />-->
                <td><textarea cols="130" name="content" rows="6" class="xheditor wu-text3" rows="20"></textarea></td>
            </tr>
        </table>
   </form>
</div>
    <script>

		//树
    	$('#tt').tree({
    		animate:true,
		    url:'http://localhost:3000/cate/list',
		    method:'get',
		    onClick: function(node){
		    	console.log(node)
				initList(node._id)
			}
		});

		function initList(cateId){
			var postUrl;
			if(cateId !=undefined && cateId.trim().length>0){
				queryParams = {cateId: cateId}
			}else{
				queryParams = {};
			}					
			$("#dga").datagrid({
				url:'http://localhost:3000/news/list',
				method:'post',
				pagination:true,
				queryParams: queryParams,
//				fit:true,
				columns:[[
			        {field:'title',title:'标题',width:128},
			        {field:'author',title:'作者',width:128},
			        {field:'subhead',title:'副标题',width:128},
			        {field:'content',title:'内容',width:128},
			        {field:'data',title:'时间',width:128},
			        {field:'Games',title:'所属分类',width:128,
			        formatter: function(value,row,index){
						return '<a href="javascript:void(0)" style="display:inline-block;padding:0px 5px;cursor: pointer;outline: none;text-align: center; ertical-align: middle;line-height: normal;border: 1px solid #bbb;color: #444;background: #fafafa;background: linear-gradient(to bottom,#ffffff 0,#eeeeee 100%);border-radius: 5px 5px 5px 5px" onclick="editUser(\''+ row._id+'\')">修改</a> '+
						'<a href="javascript:void(0)" style="display:inline-block;padding:0px 5px;cursor: pointer;outline: none;text-align: center; ertical-align: middle;line-height: normal;border: 1px solid #bbb;color: #444;background: #fafafa;background: linear-gradient(to bottom,#ffffff 0,#eeeeee 100%);border-radius: 5px 5px 5px 5px" onclick="removeUser(\''+ row._id+'\')">删除 </a>' + 
						'<a href="javascript:void(0)" style="display:inline-block;padding:0px 5px;cursor: pointer;outline: none;text-align: center; ertical-align: middle;line-height: normal;border: 1px solid #bbb;color: #444;background: #fafafa;background: linear-gradient(to bottom,#ffffff 0,#eeeeee 100%);border-radius: 5px 5px 5px 5px" onclick="addTab('+"'评论管理模块'"+','+"'temp/layout-5.html?id="+row._id+"'"+',null,1)"> 评论</a>'+
						'<a href="javascript:void(0)" style="display:inline-block;padding:0px 5px;cursor: pointer;outline: none;text-align: center; ertical-align: middle;line-height: normal;border: 1px solid #bbb;color: #444;background: #fafafa;background: linear-gradient(to bottom,#ffffff 0,#eeeeee 100%);border-radius: 5px 5px 5px 5px" onclick="addTab('+"'新闻详情模块'"+','+"'temp/layout-4.html?id="+row._id+"'"+',null,1)"> 查看详情</a>'
						}}			        
			    ]]			    
			})		
		}	
		initList();


//方法1
//		function deleUser(){
//  		//先找到选中的那一个
//  		var node = $('#dga').datagrid('getSelected');
//  		var id=node._id;
//  		//进行ajax,找出那个，在进行删除
//  		$.ajax({
//  			type:'delete',
//  			url:"http://localhost:3000/news/data/"+id
//
//  		}).then(function(res){
//  			$('#dg').datagrid('reload')
//  		})
//  	}

		function deleUser(id){
			$.ajax({
				type:"delete",
				url:"http://localhost:3000/news/data/"+ id
			}).then(function(res){
				$('#dga').datagrid('reload');
			});
		}



		//跳转
			
		
		function addTab(title, href, iconCls, iframe){
			var tabPanel =parent.$('#wu-tabs');
			if(!tabPanel.tabs('exists',title)){
				var content = '<iframe scrolling="auto" frameborder="0"  src="'+ href +'" style="width:100%;height:100%;"></iframe>';
				if(iframe){
					tabPanel.tabs('add',{
						title:title,
						content:content,
						iconCls:iconCls,
						fit:true,
						cls:'pd3',
						closable:true
					});
				}else{
					tabPanel.tabs('add',{
						title:title,
						href:href,
						iconCls:iconCls,
						fit:true,
						cls:'pd3',
						closable:true
					});
				}
			}
			else
			{
				tabPanel.tabs('select',title);
				var current_tab =tabPanel.tabs('getSelected'); //获得当前tab
				    tabPanel.tabs('update', {
				      tab : current_tab,
				      options : {
				       content :'<iframe scrolling="auto" frameborder="0" src="'+href+'"style="width:100%;height:100%;"></iframe>'
				      }
				});
			}		
		}


function openUserAdd(){
		$("#user-form").form("clear");
		$('#user-dialog').dialog({
			closed: false,
			modal:true,
            title: "添加信息",
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: userAddAction
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#user-dialog').dialog('close');                    
                }
            }]
        });
}
//添加	
function userAddAction(){
	$('#user-form').form('submit',{
        onSubmit:function(){
        	console.log($("#user-form").serialize)
            if($(this).form('enableValidation').form('validate')){
            	$.ajax({
            		type:"post",
            		url:"http://localhost:3000/news/data",
            		data:$("#user-form").serializeJSON()
            	}).then(function(res){
            		console.log(res)
            	})
            }else{
            	alert("no")
            }
        }
    });
    $('#user-dialog').dialog("close");
    $('#dga').datagrid("reload")
//	console.log("form post")
//	console.log($("#user-form").serializeArray())
}

//删除
function removeUser(){
	var node = $('#dga').datagrid('getSelected');
	var id=node._id;
	$.ajax({
		type:"delete",
		url:"http://localhost:3000/news/data/"+id
	}).then(function(res){
		$('#dga').datagrid("reload")
	})
}
function reloadUser(){
	$('#dga').datagrid("reload")
}


//修改
function openUserEdit(){
	$("#user-form").form("clear");
		$('#user-dialog').dialog({			
			closed: false,
			modal:true,
            title: "修改信息",
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: userEditAction
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#user-dialog').dialog('close');                    
                }
            }]
        });
        var node = $('#dga').datagrid('getSelected');
			console.log(node)
		var id=node._id;
        $.ajax({
			type:"post",   
			url:"http://localhost:3000/news/data/"+id
		}).then(function(res){
			$(".wu-text0").val(node.title);
			$(".wu-text1").val(node.author);
			$(".wu-text2").val(node.subhead);
			$(".wu-text3").val(node.content);
		})
}
function userEditAction(){
	var node = $('#dga').datagrid('getSelected');
	var id=node._id;
	var postUrl;
	if($(".wu-text").val()){
		$(".wu-text0").val();
		$(".wu-text1").val();
		$(".wu-text2").val();
		$(".wu-text3").val()
	}else{
		$(".wu-text0").val(node.title);
		$(".wu-text1").val(node.author);
		$(".wu-text2").val(node.subhead);
		$(".wu-text3").val(node.content)
		console.log($(".wu-text0").val())
	}
	var formData = $('#user-form').serializeJSON();
	console.log(formData)
	$.ajax({
		type:"post",
		url:"http://localhost:3000/news/data/"+id,
		data: formData
	}).then(function(res){
		$('#user-dialog').dialog("close");
		$('#dga').datagrid("reload")
	})
}

//修改
function editUser(cateId){
	console.log(cateId)
	queryParams ={cateId:cateId}
	console.log(queryParams)

	$('#user-dialog').dialog({			
			closed: false,
			modal:true,
            title: "修改信息",
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: userEditAction
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#user-dialog').dialog('close');                    
                }
            }]
        });
 
//      $.ajax({
//			type:"post",   
//			url:"http://localhost:3000/users/data/"+id
//			
//		})		
}

	    </script>
</body>
</html>
